pip3 install http://download.pytorch.org/whl/torch-0.2.0.post3-cp36-cp36m-macosx_10_7_x86_64.whl
pip3 install torchvision
In [37]:
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torch.utils.data as data
import torchvision.transforms as transforms
import torchvision.datasets as dsets
from torch.autograd import Variable
In [10]:
# torch.rand(sizes) -> [0,1]
x = torch.rand(2,3)
x
Out[10]:
In [11]:
# torch.randn(sizes) -> normalize Z[0, 1]
y = torch.randn(2,3)
y
Out[11]:
In [12]:
# torch.randperm(n) : permutation of 0-n
z = torch.randperm(5)
z
# return이 LongTensor type이네요..!
Out[12]:
In [13]:
# torch.zeros -> np.zeros와 동일
x = torch.zeros(2,3)
x
Out[13]:
In [15]:
# torch.ones(2,3) -> np.ones와 동일
y = torch.ones(2,3)
y
Out[15]:
In [17]:
# torch.arange(start, end, step)
z = torch.arange(0,2, step=0.3)
z
Out[17]:
In [23]:
# torch.FloatTensor(size or list)
x = torch.FloatTensor(2,3)
x
Out[23]:
In [19]:
x = torch.FloatTensor([2,3])
x
Out[19]:
In [25]:
x = torch.FloatTensor([[2,3],[3,3]])
x
Out[25]:
In [26]:
# torch.type_as(tensor_type)
x = x.type_as(torch.IntTensor())
x
Out[26]:
In [31]:
# torch.LongTensor
torch.LongTensor(1,2)
Out[31]:
In [6]:
# create tensors
x = Variable(torch.Tensor([1]), requires_grad=True)
In [7]:
x
Out[7]:
In [32]:
import numpy as np
In [33]:
# torch.from_numpy(np객체)
x1 = np.ndarray(shape=(2,3), dtype=int,buffer=np.array([1,2,3,4,5,6]))
x2 = torch.from_numpy(x1)
x2
Out[33]:
In [37]:
# tensor.size() : indexing
x = torch.FloatTensor(10,12,3,3)
x.size()[:]
Out[37]:
In [38]:
x.size()[1:]
Out[38]:
In [40]:
x = torch.rand(4,3)
out = torch.index_select(x,0,torch.LongTensor([0,3]))
x,out
Out[40]:
In [42]:
x[:,0]
Out[42]:
In [43]:
x[:,:]
Out[43]:
In [45]:
x[1:2,:]
Out[45]:
In [46]:
# torch.masked_select(input, mask)
x = torch.randn(2,3)
mask = torch.ByteTensor([[0,0,1],[0,1,0]])
out = torch.masked_select(x,mask)
x, mask, out
Out[46]:
In [47]:
mask
Out[47]:
In [48]:
# torch.cat(seq, dim=0) : concat
x = torch.FloatTensor([[1,2,3], [4,5,6]])
y = torch.FloatTensor([[10,11,12],[-1,-2,-3]])
In [50]:
z1 = torch.cat([x,y], dim=0)
z1
Out[50]:
In [51]:
z2 = torch.cat([x,y], dim=1)
z2
Out[51]:
In [52]:
# torch.stack(sequence, dim=0) : stack along new dim
x = torch.FloatTensor([[1,2,3], [4,5,6]])
x_stack = torch.stack([x,x,x,x], dim=0)
x_stack
Out[52]:
In [53]:
# torch.chunk(tensor,cunks, dim)
x_1, x_2 = torch.chunk(z1, 2, dim=0)
In [57]:
z1
Out[57]:
In [54]:
x_1, x_2
Out[54]:
In [55]:
y_1, y_2, y_3 = torch.chunk(z1,3,dim=1)
In [56]:
y_1, y_2, y_3
Out[56]:
In [58]:
# torch.splie(tensor, split_size, dim)
x1, x2 = torch.split(z1, 2, dim=0)
In [63]:
z1
Out[63]:
In [59]:
x1, x2
Out[59]:
In [61]:
y1 = torch.split(z1, 2, dim=1)
In [62]:
y1
Out[62]:
In [68]:
# torch.squeeze(input, dim) : 1차원 tensor 제거
x1 = torch.FloatTensor(10,1,3)
x1
Out[68]:
In [69]:
x2 = torch.squeeze(x1)
x2
Out[69]:
In [70]:
x1.size(), x2.size()
Out[70]:
In [72]:
# torch.unsqueeze(input,dim=None) -> 1차원 추가
x1 = torch.FloatTensor(10,3,4)
x2 = torch.unsqueeze(x1,dim=0)
x1.size(),x2.size()
Out[72]:
In [73]:
# torch.add() : x1+x2와 같은 연산
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
add = torch.add(x1,x2)
x1,x2,add,x1+x2,x1-x2
Out[73]:
In [74]:
# torch.add() : broadcasting 연산도 가능
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.add(x1,10)
x1,x2,x1+10,x2-10
Out[74]:
In [76]:
# torch.mul()
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
x3 = torch.mul(x1,x2)
x3, x1*10
Out[76]:
In [77]:
# torch.div() -> size better match
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
x3 = torch.div(x1,x2)
x3, x1/5
Out[77]:
In [78]:
# torch.pow(inpue, exponent) : 제곱
x1 = torch.FloatTensor(3,4)
torch.pow(x1,2),x1**2
Out[78]:
In [79]:
# torch.exp(tensor,out=None)
x1 = torch.FloatTensor(3,4)
torch.exp(x1)
Out[79]:
In [80]:
# torch.log(input, out=None)
x1 = torch.FloatTensor(3,4)
torch.log(x1)
Out[80]:
In [81]:
# torch.mm(mat1, mat2)
x1 = torch.FloatTensor(3,4)
x2 = torch.FloatTensor(4,5)
torch.mm(x1,x2)
Out[81]:
In [82]:
# torch.bmm(batch1, batch2) -> batch matrix multiplication
x1 = torch.FloatTensor(10,3,4)
x2 = torch.FloatTensor(10,4,5)
torch.bmm(x1,x2).size()
Out[82]:
In [84]:
torch.bmm(x1,x2)
Out[84]:
In [96]:
# torch.dot([tensor1,tensor2]) -> dot product of two tensor
x1 = torch.FloatTensor([1,1])
x2 = torch.FloatTensor([1,3])
torch.dot(x1,x2)
Out[96]:
In [97]:
x1 = torch.FloatTensor([1,1,3])
x2 = torch.FloatTensor([1,3,10])
torch.dot(x1,x2)
Out[97]:
In [98]:
# torch.t(matrix) -> transposed matrix
x1 = torch.FloatTensor(3,4)
x1,x1.t()
Out[98]:
In [99]:
# torch.transpose(input,dim0,dim1) -> transposed matrix
x1 = torch.FloatTensor(10,3,4)
x1.size(), torch.transpose(x1,1,2).size(), x1.transpose(1,2).size()
Out[99]:
In [5]:
# torch.eig(a,eigenvectors=False) -> eigen_value, eigen_vector
x1 = torch.FloatTensor(3,3)
x1,torch.eig(x1,True)
Out[5]:
In [12]:
x = Variable(torch.Tensor([1]), requires_grad=True)
w = Variable(torch.Tensor([2]), requires_grad=True)
b = Variable(torch.Tensor([3]), requires_grad=True)
# requires_grad = False를 하면 backward() 학습이 안됨
In [13]:
x
Out[13]:
In [14]:
y = w * x + b
In [15]:
y.backward()
In [16]:
print(x.grad)
In [17]:
print(w.grad)
In [18]:
print(b.grad)
In [19]:
x = Variable(torch.randn(5, 3))
y = Variable(torch.randn(5, 2))
In [20]:
linear = nn.Linear(3,2)
In [22]:
print ('w: ', linear.weight)
print ('b: ', linear.bias)
In [23]:
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)
In [26]:
pred = linear(x)
In [27]:
loss = criterion(pred, y)
print('loss: ', loss.data[0])
In [29]:
loss.backward()
In [30]:
# Print out the gradients.
print ('dL/dw: ', linear.weight.grad)
print ('dL/db: ', linear.bias.grad)
In [31]:
optimizer.step()
In [32]:
x
Out[32]:
In [34]:
y
Out[34]:
In [33]:
pred = linear(x)
loss = criterion(pred, y)
print('loss after 1 step optimization: ', loss.data[0])
In [38]:
%%time
# Download and construct dataset.
train_dataset = dsets.CIFAR10(root='../data/',
train=True,
transform=transforms.ToTensor(),
download=True)
In [39]:
image, label = train_dataset[0]
In [43]:
print(image.size())
In [44]:
print(label)
In [49]:
# torch.utils.data.Dataloader
# Data loader. Combines a dataset and a sampler,
# and provides single- or multi-process iterators over the dataset.
# dataset (Dataset) – dataset from which to load the data.
# batch_size (int, optional) – how many samples per batch to load (default: 1).
# shuffle (bool, optional) – set to True to have the data reshuffled at every epoch (default: False).
# sampler (Sampler, optional) – defines the strategy to draw samples from the dataset. If specified, shuffle must be False.
# batch_sampler (Sampler, optional) – like sampler, but returns a batch of indices at a time. Mutually exclusive with batch_size, shuffle, sampler, and drop_last.
# num_workers (int, optional) – how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process (default: 0)
# collate_fn (callable, optional) – merges a list of samples to form a mini-batch.
# pin_memory (bool, optional) – If True, the data loader will copy tensors into CUDA pinned memory before returning them.
# drop_last (bool, optional) – set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size.
# If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default: False)
In [46]:
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=100,
shuffle=True,
num_workers=2)
In [50]:
# When iteration starts, queue and thread start to load dataset from files.
data_iter = iter(train_loader)
In [53]:
images, labels = data_iter.next()
In [55]:
for images, labels in train_loader:
pass
# training code will be wirtten here
In [56]:
#===================== Input pipline for custom dataset =====================#
# You should build custom dataset as below.
class CustomDataset(data.Dataset):
def __init__(self):
# TODO
# 1. Initialize file path or list of file names.
pass
def __getitem__(self, index):
# TODO
# 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).
# 2. Preprocess the data (e.g. torchvision.Transform).
# 3. Return a data pair (e.g. image and label).
pass
def __len__(self):
# You should change 0 to the total size of your dataset.
return 0
In [57]:
# Then, you can just use prebuilt torch's data loader.
custom_dataset = CustomDataset()
train_loader = torch.utils.data.DataLoader(dataset=custom_dataset,
batch_size=100,
shuffle=True,
num_workers=2)
In [ ]:
#========================== Using pretrained model ==========================#
# Download and load pretrained resnet.
resnet = torchvision.models.resnet18(pretrained=True)
# If you want to finetune only top layer of the model.
for param in resnet.parameters():
param.requires_grad = False
# Replace top layer for finetuning.
resnet.fc = nn.Linear(resnet.fc.in_features, 100) # 100 is for example.
# For test.
images = Variable(torch.randn(10, 3, 256, 256))
outputs = resnet(images)
print (outputs.size()) # (10, 100)
In [ ]:
#============================ Save and load the model ============================#
# Save and load the entire model.
torch.save(resnet, 'model.pkl')
model = torch.load('model.pkl')
# Save and load only the model parameters(recommended).
torch.save(resnet.state_dict(), 'params.pkl')
resnet.load_state_dict(torch.load('params.pkl'))